'''
Description: 
version: 
Author: aps.auto
Date: 2025-03-02 18:39:30
LastEditors: Luoxl
LastEditTime: 2025-03-16 09:33:13
'''
import subprocess
import time

# 执行命令并在2秒后发送回车符
# def execute_command_with_enter(command):
#     # 使用Popen启动命令
#     process = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    
#     # 等待2秒钟
#     time.sleep(10)
    
#     # 向子进程的标准输入发送回车符
#     process.stdin.write('\n')
#     process.stdin.flush()
    
#     # 等待命令执行结束
#     stdout, stderr = process.communicate()
    
#     # 打印命令的输出
#     print("Output:", stdout)
    
#     # 如果有错误输出，打印错误信息
#     if stderr:
#         print("Error:", stderr)

def execute_command_with_enter(command):
    process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    time.sleep(10)
    process.stdin.write('\n')
    process.stdin.close()

    # 非阻塞读取输出
    while process.poll() is None:
        line = process.stdout.readline()  # 逐行读取（非阻塞）
        if line:
            print("Subprocess Output:", line.strip())
        time.sleep(0.1)  # 适当释放 GIL

    # 获取剩余输出
    remaining_stdout, remaining_stderr = process.communicate()

if __name__ == "__main__":
    # 调用函数执行命令
    # execute_command_with_enter("airbot_set_zero -m can0")
    execute_command_with_enter("airbot_set_zero -m can1")
